export const findParentCodeList = (list, rowCode) => {
  let result = [rowCode];
  function find(data, code) {
    for (let i = 0; i < data.length; i++) {
      if (data[i].code == code) {
        return data[i].parentCode;
      }
    }
    return "";
  }
  let parentCode = find(list, rowCode); //第一次
  if (parentCode !== "") {
    result.push(parentCode);
  } else {
    return result;
  }
  for (;;) {
    parentCode = find(list, parentCode);
    if (parentCode !== "") {
      result.push(parentCode);
    }
    if (parentCode === "") {
      break;
    }
  }
  result.reverse();
  return result;
};

export function formatTree(data, parentCode) {
  let result = [];
  data.forEach((item) => {
    if (item.parentCode === parentCode) {
      let children = formatTree(data, item.code);
      if (children.length === 0) {
        children = null;
      }
      item.children = children;
      result.push(item);
    }
  });
  return result;
}
